package com.soul.dubbo.api.mongo;

import com.soul.model.mongo.RecommendUser;
import com.soul.model.mongo.UserLike;
import com.soul.model.vo.PageResult;
import com.soul.model.vo.RecommendUserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findTodayBestByRecommendUser(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("toUserId").is(userId));
        query.with(Sort.by(Sort.Direction.DESC, "score"));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 首页推荐佳人分页列表 查询同登录用户符合度相符合用户
     *
     * @param recommendUserVo 参数
     * @param userId          当前登录用户id
     * @return PageResult<RecommendUser>
     */
    @Override
    public PageResult<RecommendUser> findpageRecommendation(RecommendUserVo recommendUserVo, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("toUserId").is(userId));
        // 查询总条数
        long count = mongoTemplate.count(query, RecommendUser.class);
        // 查询符合登录用户的数据
        query.limit(recommendUserVo.getPagesize().intValue()).skip((recommendUserVo.getPage() - 1) * recommendUserVo.getPagesize());
        query.with(Sort.by(Sort.Direction.DESC,"date"));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult<>(recommendUserVo.getPage(), recommendUserVo.getPagesize(), count, recommendUserList);
    }

    /**
     * 根据登录用户id和佳人id获取缘分值
     *
     * @param userId
     * @param personUserId
     * @return
     */
    @Override
    public RecommendUser findByUser(Long userId, Long personUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(personUserId).and("toUserId").is(userId));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 根据当前用户id查询推荐卡片
     *
     * @param currendUserId 当前登录用户
     * @return List<RecommendUser>
     */
    @Override
    public List<RecommendUser> findCards(Long currendUserId) {
        // 1. 查询user_like集合中 喜欢和不喜欢用户
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currendUserId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        // 2. 非空 获取用户id转为ids
        List<Long> likeUserIds = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userLikeList)){
            likeUserIds = userLikeList.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        }

        // 3. 随机获取用户构造数据返回前端
        Criteria userIds = Criteria.where("toUserId").is(currendUserId).and("userId").nin(likeUserIds);
        //  参数1：构造条件对象 参数2：userIds条件对象 匹配的用户ids( 有相同用户 排除用户ids) 参数3：随机条件
        TypedAggregation<RecommendUser> rta = TypedAggregation
                .newAggregation(RecommendUser.class,Aggregation.match(userIds),Aggregation.sample(10));
        //  参数1：随机的条件 参数2：随机表
        AggregationResults<RecommendUser> r = mongoTemplate.aggregate(rta, RecommendUser.class);
        return r.getMappedResults();
    }

    /**
     * 查询用户匹配度
     *
     * @param userId
     * @param visitorUserId
     * @return
     */
    @Override
    public RecommendUser findUserMatchRate(Long userId, Long visitorUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }
}
